var express = require('express');
var router = express.Router();
var mysql = require('./mysqlConfig');
var bodyParser = require("body-parser");
// var redis = require('./redisServer');
var register = require('babel-core/register')
var uuid = require('node-uuid')
router.use(bodyParser.json({
    limit: "20000kb"
}));
router.use(bodyParser.urlencoded({
    parameterLimit: 100000,
    limit: '20000kb',
    extended: true
}));

/* GET home page. */

//处理连接数据库
const connectionMysql = (sql) => {
    return new Promise(function(resolve, reject) {
        mysql.getConnection(function(err, connection) {
            if (err) {
                reject(err)
                console.log(err)
            } else {
                resolve(sqlSever(connection, sql))
            }
        });
    });
}
//处理sql语句
const sqlSever = (connection, sql) => {
    return new Promise(function(resolve, reject) {
        connection.query(sql, function(err, rows, fields) {
            if (err) {
                console.log(err)
                reject(err);
            } else {
                console.log(err)
                resolve(rows);
                connection.release();
            }
        })
    });
}

//获取最新活动列表
router.get('/getNewActivity', function(req, res, next) {

    var sql = 'select * from activities where end_time >= NOW()';
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {});
});

//获取收藏信息
router.get('/isColl', function(req, res, next) {
    var actNum = "'" + req.query.actNum + "'";
    var num = "'" + req.query.num + "'";
    var sql = 'select * from mem_collection where acti_no = ' + actNum + 'and mem_id=' + num;
    console.log(sql)
    connectionMysql(sql)
        .then((result) => {
            if (result.length == 0) {
                var json = {
                    code: 200,
                    data: "未关注",
                    list: result
                }
                res.send(json);
            } else {
                var json = {
                    code: 200,
                    data: "已关注",
                    list: result
                }
                res.send(json);
            }

        })
        .catch((reason) => {});
});

//收藏
router.get('/type', function(req, res, next) {
    var cid = "'" + uuid.v4() + "'";
    var coll_type = "'" + req.query.coll_type + "'";
    var id = "'" + req.query.id + "'";
    var actNum = "'" + req.query.actNum + "'";
    var sql = 'insert into mem_collection (cid,coll_type,acti_no,mem_id) values (' + cid + ',' + coll_type + ',' + actNum + ',' + id + ')';
    console.log(sql)
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {
            var json = {
                code: 400

            }
            res.send(json);
        });
});

//取消收藏
router.get('/cancelColl', function(req, res, next) {
    var id = "'" + req.query.id + "'";
    var actNum = "'" + req.query.actNum + "'";
    var sql = 'delete from mem_collection where acti_no = ' + actNum + ' and mem_id=' + id;
    console.log(sql)
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {
            var json = {
                code: 400

            }
            res.send(json);
        });
});

module.exports = router;